/*
								Headcrab Colony by Stephen Swires, Joshua Simmons and Matthew Smith
	==================================================================================================================================================
	The MIT License
	Copyright (c) 2006 Stephen Swires, Joshua Simmons and Matthew Smith

	Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in
	the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
	Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
	WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
	OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 
	OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
	==================================================================================================================================================
*/

// includes
include("shared.lua")

// cs files
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

// vars, this is DEFAULT, it can be changed through keyvalues
EggModel = "models/props_junk/watermelon01.mdl"
UniqueNo = 0

function ENT:Initialize()
	
	// this will get overwritten if keyvalues have been set
	self.Entity:SetNetworkedString( "NPC", "npc_headcrab" )
	self.Entity:SetNetworkedInt( "Time", 30 )
	
	if( self.Entity:GetNetworkedString( "Model" ) ) then
		EggModel = self.Entity:GetNetworkedString( "Model" )
	end
	
	// then apply our shit
	self.Entity:SetModel( EggModel ) // like the model
	self.Entity:PhysicsInit( SOLID_VPHYSICS ) // now all the physics crap
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	
	// then give it some fucking physics
	if(self.Entity:GetPhysicsObject():IsValid()) then
		self.Entity:GetPhysicsObject():Wake()
	end
	
	// create the timer for hatching
	timer.Create( "Hatch" .. tostring( UniqueNo ), self.Entity:GetNetworkedInt( "Time" ), 1, self.Hatch )
	self.Entity:SetNetworkedInt( "TimerIdentifier" , UniqueNo )
	UniqueNo = UniqueNo + 1
	
	// default health
	self.Entity:SetHealth( 120 )
	
end

function ENT:Hatch()

	local NPCType = string.sub( "npc_", "", self.Entity:GetNetworkedString( "NPC" ) )
	
	// create our npc
	local NewNPC = ents.Create( "npc_" .. NPCType )
	NewNPC:SetPos( self.Entity:GetPos() )
	NewNPC:SetAngle( Angle( self.Entity:GetAngle().X , 0 , 0 ) )
	
	// get rid of the egg
	self.Entity:Fire("Kill", "", 0) // get rid of our entity, we don't need it no more
	
	// spawn the NPC
	NewNPC:Spawn()
	
end

// when we get hit.
function ENT:TakeDamage( DamageInfo )
	if( self.Entity:Health() == 0 ) then
		self.Entity:Fire( "break", "", 0 )
		timer.Remove( "Hatch" .. self.Entity:GetNetworkedInt( "TimerIdentifier" ) )
	elseif( DamageInfo:GetAttacker():IsNPC() == true) then
		DamageReduced = DamageInfo:GetDamage() / 2
		self.Entity:SetHealth( self.Entity:Health() + DamageReduced )
	end
end

// entity settings, here's where mappers and coders can change shit.
function ENT:KeyValue( k, v )
	if(k == "npctype") then
		self.Entity:SetNetworkedString( "NPC", v )
	elseif(k == "time") then
		self.Entity:SetNetworkedInt( "Time", v )
	elseif(k == "model") then
		self.Entity:SetNetworkedString( "Model", v )
	end
end
